package com.wy.thread_safe.locks_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//类和对象实现加锁的代码
/**
 * @author 美女
 * @date 2022/04/25 17:08
 **/
public class Main1 {
    private static final Lock lock=new ReentrantLock();
    static class MyThread extends Thread{
        @Override
        public void run() {
            lock.lock();
            System.out.println("子线程进入临界区");//理论上这句代码永远到不了,因为这把锁已经被主线程锁上了，主线程比并未解锁
        }
    }
    public static void main(String[] args) throws InterruptedException {
        lock.lock();//主线程已锁上
        MyThread t=new MyThread();
        t.start();
        t.join();
    }
}
